package model;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class FileSearch {
	private JFileChooser fileChooser;
	private JLabel directoryLabel;
	private JLabel fileNameLabel;
	private String fileName;
	
	public FileSearch(JFrame frame) {
		directoryLabel = new JLabel();
		fileNameLabel = new JLabel();
		fileChooser = new JFileChooser(".");
    }
	
	public String ChooseFile(ActionEvent e) {
		Component parent = (Component)e.getSource();
		
		int status = fileChooser.showOpenDialog(parent);
		if (status == JFileChooser.APPROVE_OPTION) {
		    File selectedFile = fileChooser.getSelectedFile();
		    /*directoryLabel.setText(selectedFile.getParent());
		    directory = directoryLabel.getText();*/
		    fileNameLabel.setText(selectedFile.getName());
		    fileName = fileNameLabel.getText();
		    
		    return fileName;
		} 
		else if (status == JFileChooser.CANCEL_OPTION) {
			directoryLabel.setText(" ");
		    fileNameLabel.setText(" ");
		    
		    return "";
		}
		else
			return "";
    }
}
